{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "defaultdict.ipynb",
      "version": "0.3.2",
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "metadata": {
        "id": "IHv_7f-lQlun",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Usually, a Python dictionary throws a ` KeyError` if you try to get an item with a key that is not currently in the dictionary.  \n",
        "The ` defaultdict` in contrast will simply create any items that you try to access."
      ]
    },
    {
      "metadata": {
        "id": "F0gPaG-5P5U_",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "7d96a2b7-1224-4385-e7ef-61b251afd903"
      },
      "cell_type": "code",
      "source": [
        "import collections\n",
        "from collections import defaultdict\n",
        "\n",
        "# create normal python dictionary\n",
        "s = {'yellow': 1, 'blue': 2, 'green': 3, 'red': 1}\n",
        "print(s)"
      ],
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "{'yellow': 1, 'blue': 2, 'green': 3, 'red': 1}\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "zR6PIP-6SrHA",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 221
        },
        "outputId": "ae769e62-0e66-4cfa-d078-6dbb95e080a3"
      },
      "cell_type": "code",
      "source": [
        "# access using present key\n",
        "print(s['blue'])\n",
        "\n",
        "# access using missing key\n",
        "print(s['white'])"
      ],
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "2\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "error",
          "ename": "KeyError",
          "evalue": "ignored",
          "traceback": [
            "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
            "\u001b[0;31mKeyError\u001b[0m                                  Traceback (most recent call last)",
            "\u001b[0;32m<ipython-input-2-172eb2b3b778>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      3\u001b[0m \u001b[0;31m# access using missing key\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ms\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'white'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
            "\u001b[0;31mKeyError\u001b[0m: 'white'"
          ]
        }
      ]
    },
    {
      "metadata": {
        "id": "SaJr8eQoTZuH",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "ce608be5-7f37-4a6b-9707-b7ac424a8503"
      },
      "cell_type": "code",
      "source": [
        "# create default dict\n",
        "\n",
        "d = defaultdict(int, s)\n",
        "d.items()"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "dict_items([('yellow', 1), ('blue', 2), ('green', 3), ('red', 1)])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 3
        }
      ]
    },
    {
      "metadata": {
        "id": "wYU8lc0DU7Dr",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "baada121-d472-4737-e577-199577331a7b"
      },
      "cell_type": "code",
      "source": [
        "# let's access a missing key\n",
        "print(d['white'])"
      ],
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "0\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "Pz29QbWFVEt6",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "19335346-0158-4b96-cb0b-b4997f9de816"
      },
      "cell_type": "code",
      "source": [
        "# check the defaultdict\n",
        "\n",
        "d.items()"
      ],
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "dict_items([('yellow', 1), ('blue', 2), ('green', 3), ('red', 1), ('white', 0)])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 5
        }
      ]
    },
    {
      "metadata": {
        "id": "FqBi6TirVOJS",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "It is easy to group a sequence of key-value pairs into a dictionary of lists"
      ]
    },
    {
      "metadata": {
        "id": "ouLF9UabVGkA",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "06805868-8dcb-4e79-ac79-b6248968b7cb"
      },
      "cell_type": "code",
      "source": [
        "s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]\n",
        "d = defaultdict(list)\n",
        "\n",
        "for k, v in s:\n",
        "  d[k].append(v)\n",
        "  \n",
        "d.items()"
      ],
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "dict_items([('yellow', [1, 3]), ('blue', [2, 4]), ('red', [1])])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 6
        }
      ]
    },
    {
      "metadata": {
        "id": "AYJr2KkYVz_3",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "It can also be used for counting"
      ]
    },
    {
      "metadata": {
        "id": "N_m-lPftVxzk",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "6f80623b-50ee-4b41-c6ba-4ef10f54a693"
      },
      "cell_type": "code",
      "source": [
        "s = 'mississippi'\n",
        "\n",
        "d = defaultdict(int)\n",
        "for k in s:\n",
        "  d[k] += 1\n",
        "\n",
        "d.items()"
      ],
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "dict_items([('m', 1), ('i', 4), ('s', 4), ('p', 2)])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 7
        }
      ]
    }
  ]
}